import {
  getPage
} from '../../utils/config';
import wxApi from '../../utils/wxApi';
import api from '../../utils/api';
import {
  http,
  $loading,
  $toast
} from '../../utils/utils';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getPage: getPage,
    loading: 1,
    tabsActive: 0,
    pagesActive: 2,
    goodsInfo: [],
    goodsPage: 1,
    goodsdone: false,
    goodsabnor: false,
    shopInfo: [],
    shopPage: 1,
    shopcount: 0,
    shopdone: false,
    shopabnor: false,
    xuqiuInfo: [],
    xqPage: 1,
    xqcount: 0,
    xqdone: false,
    xqabnor: false,
    loadmore: false,
    right: [{
      text: '删除',
    }, {
      text: '删除',
      style: 'color: #fa3e3e',
    }]
  },

  /**
   * 切换显示
   */
  tabsClick(e) {
    const {
      index
    } = e.detail;
    console.log('index================', index)
    const {
      goodsInfo,
      shopInfo,
      xuqiuInfo,
      logged
    } = this.data;

    if (logged) {
      //获取店铺收藏
      if (index == 0 && shopInfo.length == 0) {
        this.getShopCollectionInfo((res) => {
          this.setData({
            shopInfo: res
          })
        });
      }

      //获取商品店铺
      if (index == 1 && goodsInfo.length == 0) {
        console.log('商品---------')
        this.getGoodsCollectioInfo((res) => {
          this.setData({
            goodsInfo: res
          })
        });
      }
      //获取收藏发布
      if (index == 2 && xuqiuInfo.length == 0) {
        console.log('需求---------')
        this.getXuqiuCollectionInfo((res) => {
          this.setData({
            xuqiuInfo: res
          })
        });
      }
    }

    this.setData({
      tabsActive: index
    });
  },

  /**
   * 跳转商品详情
   */
  visitGoods(e) {
    const {
      id,
      index,
      open
    } = e.currentTarget.dataset || e.target.dataset;
    if (open) {
      this.toggleSwipe(e, false);
    } else {
      wxApi.navigateTo(getPage.productdetail, {
        id: id
      });
    }
  },

  /**
   * 跳转需求详情
   */
  visitXq(e) {
    const {
      xuqiuid,
      index,
      open
    } = e.currentTarget.dataset || e.target.dataset;
    if (open) {
      this.toggleSwipe(e, false);
    } else {
      wxApi.navigateTo(getPage.xuqiudetail, {
        id: xuqiuid
      });
    }
  },

  /**
   * 跳转店铺
   */
  visitStore(e) {
    const {
      id,
      index,
      shopId,
      open
    } = e.currentTarget.dataset || e.target.dataset;
    if (open) {
      this.toggleSwipe(e, false);
    } else {
      wxApi.navigateTo(getPage.companydetail, {
        id: shopId
      });
    }
  },

  /**
   * 开启
   */
  onOpen(e) {
    this.toggleSwipe(e, true);
  },

  toggleSwipe(e, t) {
    const data = e.currentTarget.dataset || e.target.dataset;
    const {
      goodsInfo,
      shopInfo
    } = this.data;

    if (data.goodsId !== undefined) {
      goodsInfo[data.index].isOpen = t;
      this.setData({
        goodsInfo
      });
    } else if (data.shopId !== undefined) {
      shopInfo[data.index].isOpen = t;
      this.setData({
        shopInfo
      });
    }
  },

  /**
   * 商品收藏列表
   */
  getGoodsCollectioInfo(callback, a = false) {
    http.post(api.default, {
      data: {
        action: 'favorite_list',
        page: this.data.goodsPage,
        userid: this.data.userId,
      }
    }, a).then((res) => {
      const data = res.data.data;
      const goodsInfo = data.list;
      const pages = Math.ceil(data.procount / 10);

      /**
       * 增加判断是否打开
       */
      goodsInfo.forEach((item) => {
        item.isOpen = false
      })

      //记录数据
      this.setData({
        goodsPages: pages,
        shopcount: data.shopcount,
        procount: data.procount,
        xqcount: data.xqcount,
        goodsabnor: pages == 0 ? true : false,
      });

      callback && callback(goodsInfo);

      this.setData({
        goodsdone: pages == 1 ? true : false,
      })
    });
  },

  /**
   * 店铺收藏列表
   */
  getShopCollectionInfo(callback, a = false) {
    http.post(api.default, {
      data: {
        action: 'favorite_shop_list',
        userid: this.data.userId,
        page: this.data.shopPage,
      }
    }, a).then((res) => {
      const data = res.data.data;
      const shopInfo = data.list;
      const pages = Math.ceil(data.shopcount / 10);

      /**
       * 增加判断是否打开
       */
      data.list.forEach((item) => {
        item.isOpen = false
      })

      //记录数据
      this.setData({
        shopPages: pages,
        shopcount: data.shopcount,
        procount: data.procount,
        xqcount: data.xqcount,
        shopabnor: pages == 0 ? true : false,
      });

      callback && callback(shopInfo);

      this.setData({
        shopdone: pages == 1 ? true : false,
      })
    });
  },

  /**
   * 店铺收藏列表
   */
  getXuqiuCollectionInfo(callback, a = false) {
    http.post(api.default, {
      data: {
        action: 'favorite_xuqiu_list',
        userid: this.data.userId,
        page: this.data.xqPage,
      }
    }, a).then((res) => {
      const data = res.data.data;
      const xuqiuInfo = data.list;
      const pages = Math.ceil(data.xqcount / 10);

      /**
       * 增加判断是否打开
       */
      data.list.forEach((item) => {
        item.isOpen = false
      })

      //记录数据
      this.setData({
        xqPages: pages,
        shopcount: data.shopcount,
        procount: data.procount,
        xqcount: data.xqcount,
        xqabnor: pages == 0 ? true : false,
      });

      callback && callback(xuqiuInfo);

      this.setData({
        xqdone: pages == 1 ? true : false,
      })
    });
  },

  /**
   * 删除事件
   */
  handleDelete(e) {
    const that = this;
    const data = e.currentTarget.dataset || e.target.dataset;
    const {
      tabsActive
    } = that.data;
    wx.showModal({
      cancelText: '取消',
      confirmText: '确定',
      content: '是否取消收藏？',
      success(res) {
        if (res.confirm) {
          if (Number(tabsActive) == 1) {
            const goodsInfo = that.data.goodsInfo;
            http.post(api.default, {
              data: {
                action: 'favorite_remove',
                userid: that.data.userId,
                prid: data.productid,
              }
            }).then((res) => {
              goodsInfo.forEach((item, index) => {
                if (item.productid == data.productid) {
                  goodsInfo.splice(index, 1);
                }
              });
              $toast(`取消收藏成功`, {
                icon: 'success'
              });
              that.setData({
                goodsInfo: goodsInfo,
                goodsabnor: goodsInfo.length == 0 ? true : false,
                goodsdone: goodsInfo.length == 0 ? false : true,
              })
            })
          } else if (Number(tabsActive) == 0) {
            const shopInfo = that.data.shopInfo;
            http.post(api.default, {
              data: {
                action: 'del_fav_shop',
                userid: that.data.userId,
                shopid: data.shopId,
              }
            }).then((res) => {
              shopInfo.forEach((item, index) => {
                if (item.ShopId == data.shopId) {
                  shopInfo.splice(index, 1);
                }
              });
              $toast(`取消收藏成功`, {
                icon: 'success'
              });
              that.setData({
                shopInfo: shopInfo,
                shopabnor: shopInfo.length == 0 ? true : false,
                shopdone: shopInfo.length == 0 ? false : true,
              })
            })
          }else if (Number(tabsActive) == 2) {
            const xuqiuInfo = that.data.xuqiuInfo;
            http.post(api.default, {
              data: {
                action: 'del_fav_xuqiu',
                userid: that.data.userId,
                xuqiuid: data.xuqiuid,
              }
            }).then((res) => {
              xuqiuInfo.forEach((item, index) => {
                if (item.xuqiuid == data.xuqiuid) {
                  xuqiuInfo.splice(index, 1);
                }
              });
              $toast(`取消收藏成功`, {
                icon: 'success'
              });
              that.setData({
                xuqiuInfo: xuqiuInfo,
                xqabnor: xuqiuInfo.length == 0 ? true : false,
                xqdone: xuqiuInfo.length == 0 ? false : true,
              })
            })
          }
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //判断是否带参数
    if (typeof (options.id) !== 'undefined') {
      console.log(options.id)
      this.setData({
        tabsActive: options.id
      })
    }
    //判断是否登录
    app.getLogin().then((res) => {
      this.setData({
        logged: res.code,
        userId: res.data.user.user_id,
        userRank: res.data.user.user_rank,
      });
      const {
        tabsActive
      } = this.data;
      //获取店铺收藏
      if (tabsActive == 0) {

        this.getShopCollectionInfo((res) => {
          this.setData({
            shopInfo: res
          })
        });
      }

      //获取商品店铺
      if (tabsActive == 1) {
        this.getGoodsCollectioInfo((res) => {
          this.setData({
            goodsInfo: res
          })
        });
      }
      //获取发布
      if (tabsActive == 2) {
        this.getXuqiuCollectionInfo((res) => {
          this.setData({
            xuqiuInfo: res
          })
        });
      }
    }).catch((res) => {
      this.setData({
        logged: res
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    //商品下拉加载
    if (this.data.tabsActive == 1 && !this.data.loadmore) {
      const pages = this.data.goodsPages;
      let page = this.data.goodsPage;
      page++;
      if (pages >= page) {
        this.setData({
          goodsPage: page,
          loadmore: true,
        });

        //拉取数据
        this.getGoodsCollectioInfo((res) => {
          const goodsInfo = this.data.goodsInfo;
          this.setData({
            goodsInfo: goodsInfo.concat(res),
            loadmore: false,
            goodsdone: pages == page ? true : false,
          });
        }, false);
      }
    } else if (this.data.tabsActive == 0) {
      const pages = this.data.shopPages;
      let page = this.data.shopPage;
      page++;
      if (pages >= page) {
        this.setData({
          shopPage: page,
          loadmore: true,
        });

        //拉取数据
        this.getShopCollectionInfo((res) => {
          const shopInfo = this.data.shopInfo;
          this.setData({
            shopInfo: shopInfo.concat(res),
            loadmore: false,
            shopdone: pages == page ? true : false,
          });
        }, false);
      }
    } else if (this.data.tabsActive == 2) {
      const pages = this.data.xqPages;
      let page = this.data.xqPage;
      page++;
      if (pages >= page) {
        this.setData({
          xqPage: page,
          loadmore: true,
        });

        //拉取数据
        this.getXuqiuCollectionInfo((res) => {
          const xuqiuInfo = this.data.xuqiuInfo;
          this.setData({
            xuqiuInfo: xuqiuInfo.concat(res),
            loadmore: false,
            xqdone: pages == page ? true : false,
          });
        }, false);
      }
    }
  },
})